package com.guigu.guilimall.auth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.utils.R;
import com.guigu.guilimall.auth.entity.ao.ProviderToken;
import com.guigu.guilimall.auth.entity.ao.SociUser;
import com.atguigu.common.vo.MemberRespVo;
import com.guigu.guilimall.auth.feign.MemberFeignService;
import com.guigu.guilimall.auth.until.GiteeProvider;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
@Log4j2
@Controller
public class AuthController {

    @Autowired
    private MemberFeignService memberFeignService;

    @RequestMapping("/auth/gitee/success")
    public String giTee(@RequestParam("code") String code, HttpSession session, HttpServletResponse httpResponse){
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> body = new HashMap<String, String>();
        body.put("code", code);
        body.put("client_id", "33c395462168e057bf3949340e277b9de9fd24adba25ca9e2ce12adf8ad53946");  //注意，CST_ptdie100该模板ID仅为调试使用，调试结果为"status": "OK" ，即表示接口调用成功，然后联系客服报备自己的专属签名模板ID，以保证短信稳定下发
        body.put("redirect_uri", "http://localhost:9090/auth/gitee/success");
        body.put("client_secret", "96384af081c13b32f43e1392c388317cb9e8633b39a420b6c1661256d06417d6");

            ProviderToken token=new ProviderToken();
            token.setCode(code);
            token.setClientId("33c395462168e057bf3949340e277b9de9fd24adba25ca9e2ce12adf8ad53946");
            token.setRedirectUri("http://localhost:9090/auth/gitee/success");
            token.setClientSecret("96384af081c13b32f43e1392c388317cb9e8633b39a420b6c1661256d06417d6");
        //    SociUser giteeToken = GiteeProvider.getGiteeToken(token);
       //     log.info("第三方获取到的token={}", JSON.toJSONString(giteeToken));
        SociUser giteeToken=new SociUser();
        giteeToken.setCreated_at(1713682755);
        R r = memberFeignService.authLogin(giteeToken);
            if (r.getCode()==0){
                MemberRespVo data = r.getData("data", new TypeReference<MemberRespVo>() {
                });
                log.info("第三方用户登录信息 vo={}", JSON.toJSONString(data));
                session.setAttribute("login",data);
            /*    Cookie cookie=new Cookie("JESSION","");
                httpResponse.addCookie(Cookie);*/
                return "redirect:http://localhost:8070/index.html";
            }else {
                return "redirect:http://localhost:9090/login.html";
            }

    }

    @RequestMapping("/auth/gitee/test")
    public String testee(){
        SociUser giteeToken=new SociUser();
        giteeToken.setAccessToken("e0b90ad960ed8ed03edba44e8bce22ab");
        giteeToken.setExpiresIn(86400);
        giteeToken.setCreated_at(1713681749);
        R r = memberFeignService.authLogin(giteeToken);
        return "ok";
    }


}
